package org.example.controller;

import org.dmg.pmml.FieldName;
import org.dmg.pmml.PMML;
import org.jpmml.evaluator.*;
import org.jpmml.model.PMMLUtil;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.xml.sax.SAXException;

import javax.xml.bind.JAXBException;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/pmnl")
@RestController
public class PMMLController {


    ///http://localhost:9999/carapi/pmnl/predict?features=5,0,1199,603,15,50,1189,0,5,5
    //http://localhost:9999/carapi/pmnl/predict?features=5,0,1199,603,15,50,1189,0,5,5

    @GetMapping("predict")
    public Map<String,Object> predict(String features) throws JAXBException, IOException, SAXException {
        Map<String,Object> returnMap = new HashMap<>();

        String[] arr = features.split(",");

        Map<String, Float> map = new HashMap<String, Float>();
        map.put("age_range", Float.parseFloat(arr[0]));
        map.put("gender", Float.parseFloat(arr[1]));
        map.put("total_logs", Float.parseFloat(arr[2])); //直播间浏览用户的数据量
        map.put("unique_item_ids", Float.parseFloat(arr[3])); //浏览量
        map.put("categories", Float.parseFloat(arr[4])); //商品种类分析
        map.put("browse_days", Float.parseFloat(arr[5]));//登录用户天数数据
        map.put("one_clicks", Float.parseFloat(arr[6])); //点击用户
        map.put("shopping_carts", Float.parseFloat(arr[7])); //加购用户分析
        map.put("purchase_times", Float.parseFloat(arr[8])); //购买用户
        map.put("favourite_times", Float.parseFloat(arr[9])); //收藏用户

        //加载模型

        InputStream is = new ClassPathResource("pmnl/foresttreeclassifier.pmml").getInputStream();
        PMML unmarshal = PMMLUtil.unmarshal(is);
        ModelEvaluatorFactory modelEvaluatorFactory = ModelEvaluatorFactory.newInstance();
        Evaluator  evaluator = modelEvaluatorFactory.newModelEvaluator(unmarshal);
        evaluator.verify();
        System.out.println("模型加载成功!!!!");

        List<InputField> inputFields = evaluator.getInputFields();
        Map<FieldName, FieldValue> argements = new LinkedHashMap<>();

        for (InputField inputField : inputFields) {
            FieldName inputFieldName = inputField.getName();
            Object raeValue = map.get(inputFieldName.getValue());
            FieldValue inputFieldValue = inputField.prepare(raeValue);
            System.out.println(inputFieldName+"----------"+inputFieldValue);
            argements.put(inputFieldName, inputFieldValue);
        }

        Map<FieldName, ?> result = evaluator.evaluate(argements);

        List<TargetField> targetFields = evaluator.getTargetFields();

        ProbabilityDistribution probabilityDistribution = (ProbabilityDistribution) result.get(targetFields.get(0).getName());
        Integer res = (Integer)probabilityDistribution.getResult();

        returnMap.put("msg",res);
        return returnMap;
    }
}

